﻿package org.openChart.display 
{
	import flash.geom.Point;
	import org.openChart.meta.BarChart;
	import org.openChart.meta.Axis;
	import org.openChart.meta.TextLabel;
	import org.openChart.meta.Stick;
	import org.openChart.util.Rectangle;
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class DisplayYAxis extends DisplayAxis implements DisplayInterface
	{
		//public var axis:YAxis;
		public function DisplayYAxis( yaxis:Axis, chart:DisplayBarChart ) 
		{
			super(yaxis, chart);
		}
		
		public override function initialize():void
		{
			super.initialize();
			
			var textLabels:Array = axis.labels;
			var labelCount:uint = textLabels.length;
			
			var sticks:Array = axis.sticks;
			var sticksPerSegment:uint = 0;
			
			var i:uint = 0;
			var j:uint = 0;
			for (j = 0; j < sticks.length; j++ )
			{
				var stick:Stick = sticks[j] as Stick; 
				sticksPerSegment += stick.repeat;
			}
			
			for (i = 0; i < labelCount ; i++)
			{
				for (j = 0; j < sticks.length; j++ )
				{
					stick = sticks[j] as Stick;
					
					for (var h:uint = 0; h < stick.repeat; h++ )
					{
						var displayStick:DisplayStick = new DisplayStick(stick);
						displayStick.stickLine = new DisplayStickLine(stick.stickLine);
						this.sticks.addStick(displayStick);
					}
				}
			}
			
			for (i = 0; i < labelCount; i++) 
			{
				var label:TextLabel = textLabels[i] as TextLabel;
				var displayTextLabel:DisplayTextLabel = new DisplayTextLabel(label);
				displayTextLabel.initialize();
				displayTextLabel.align = "center-right";
				this.labels.addTextLabel(displayTextLabel);
			}
			
			if (title)
			{
				title.rotation = 270;
				title.initialize();
				title.align = "top-right";
			}
		}
		
		
		public override function resize(rectangle:Rectangle):void
		{
			super.resize(rectangle);
			
			var length:Number = Math.abs(startPoint.y - endPoint.y);
			var segmentLabel:Number = length / (labels.length - 1);
			var segmentStick:Number = length / (sticks.length - 1);
			var offset:Number = 0;
			
			var i:uint = 0;
			var displayStick:DisplayStick;
			var displayLabel:DisplayTextLabel;
			if (axis.maxValue < axis.minValue)
			{
				for (i = 0; i < sticks.length; i++ )
				{
					displayStick = sticks.getAt(i);
					displayStick.startPosition = new Point(startPoint.x, startPoint.y - (segmentLabel / 2 + i * segmentLabel));
					displayStick.endPosition = new Point(startPoint.x - displayStick.height, startPoint.y - (segmentLabel / 2 + i * segmentLabel));
				}
				
				offset += sticks.maxHeight;
				for (i = 0; i < labels.length; i++)
				{
					displayLabel = labels.getAt(i);
					displayLabel.position = new Point(startPoint.x - offset, startPoint.y - (segmentLabel / 2 + i * segmentLabel) );
				}
				
				offset += labels.maxHeight;
				title.position = new Point(startPoint.x - offset, (startPoint.y + 3 * endPoint.y) / 4);
			}
			else
			{
				for (i = 0; i < sticks.length; i++ )
				{
					displayStick = sticks.getAt(i);
					displayStick.startPosition = new Point(startPoint.x, startPoint.y - (i * segmentLabel));
					displayStick.endPosition = new Point(startPoint.x - displayStick.height, startPoint.y - (i * segmentLabel));
					if (displayStick.stickLine)
					{
						var line:DisplayStickLine = displayStick.stickLine;
						var xDispalyAxis:DisplayXAxis = parent.xDisplayAxis;
						line.startPosition.x = xDispalyAxis.startPoint.x;
						line.startPosition.y = xDispalyAxis.startPoint.y - (i * segmentLabel);
						line.endPosition.x = xDispalyAxis.endPoint.x;
						line.endPosition.y = xDispalyAxis.startPoint.y - (i * segmentLabel);
					}
				}
				
				offset += sticks.maxHeight;
				for (i = 0; i < labels.length; i++)
				{
					displayLabel = labels.getAt(i);
					displayLabel.position = new Point(startPoint.x - offset, startPoint.y - (i * segmentLabel) );
					
				}
				
				offset += labels.maxHeight;
				title.position = new Point(startPoint.x - offset, (startPoint.y + 3 * endPoint.y) / 4);
			}
		}
		
	}
	
}